using B.S.NewMedical.Api.Write.Application.Command.Order.Prescription;
using B.S.NewMedical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace B.S.NewMedical.Api.Write.Controllers.Order
{
    /// <summary>
    /// 处方控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PrescriptionController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PrescriptionController> _logger;

        public PrescriptionController(IMediator mediator, ILogger<PrescriptionController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 删除处方记录
        /// </summary>
        /// <param name="request">删除请求</param>
        /// <returns>删除结果</returns>
        [HttpGet]
        public async Task<ApiResult<int>> DeletePrescription([FromQuery] DeletePrescriptionCommand request)
        {
            _logger.LogInformation("收到删除处方记录请求，ID：{Id}", request.Id);

            try
            {
                var result = await _mediator.Send(request);

                if (result.Code == ApiEnum.Success)
                {
                    _logger.LogInformation("处方记录删除成功，ID：{Id}", request.Id);
                }
                else
                {
                    _logger.LogWarning("处方记录删除失败：{Message}，ID：{Id}", result.Msg, request.Id);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除处方记录时发生异常，ID：{Id}", request.Id);
                return new ApiResult<int>
                {
                    Code = ApiEnum.Error,
                    Msg = $"删除处方记录失败：{ex.Message}"
                };
            }
        }

        /// <summary>
        /// 批量删除处方记录
        /// </summary>
        /// <param name="request">批量删除请求</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDeletePrescription( BatchDeletePrescriptionCommand request)
        {
            _logger.LogInformation("收到批量删除处方记录请求，数量：{Count}", request.PrescriptionIds.Count);

            try
            {
                var result = await _mediator.Send(request);

                if (result.Code == ApiEnum.Success)
                {
                    _logger.LogInformation("批量删除处方记录成功，数量：{Count}", result.Data);
                }
                else
                {
                    _logger.LogWarning("批量删除处方记录失败：{Message}，数量：{Count}",
                        result.Msg, request.PrescriptionIds.Count);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "批量删除处方记录时发生异常，数量：{Count}", request.PrescriptionIds.Count);
                return new ApiResult<int>
                {
                    Code = ApiEnum.Error,
                    Msg = $"批量删除处方记录失败：{ex.Message}"
                };
            }
        }
    }
}